// 请求基础封装
const BASE_URL = 'https://feiwow.com:818/api/';
// const BASE_URL = 'http://192.168.1.102:8018/api/';

// 封装请求方法
const request = (url, method = 'GET', data = {}, header = {}) => {
  // 获取token
  const token = uni.getStorageSync('token');
  // 如果有token，自动添加到header中
  const defaultHeader = token ? { Authorization: 'Bearer ' + token } : {};

  return new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + url,
      method: method,
      data: data,
      header: { ...defaultHeader, ...header },
      success: (res) => {
        if (res.data.code === 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

// 封装GET请求
const get = (url, params = {}, header = {}) => {
  return request(url, 'GET', params, header);
};

// 封装POST请求
const post = (url, data = {}, header = {}) => {
  return request(url, 'POST', data, header);
};

export default {
  request,
  get,
  post
};
